package com.sdcqjy.property.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.sdcqjy.property.MyConstants;
import com.sdcqjy.property.R;
import com.sdcqjy.property.adapter.SelectCityAdapter;
import com.sdcqjy.property.base.SimpleBaseActivity;
import com.sdcqjy.property.mode.CityMode;
import com.sdcqjy.property.widget.WItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 磁磁帅 on 2018/4/15.
 * 功能：
 */
public class SelectCityActivity extends SimpleBaseActivity {
    @BindView(R.id.listView)
    RecyclerView listView;
    private List<CityMode> modeList;
    private SelectCityAdapter adapter;

    public static void open(Context context) {
        Intent intent = new Intent(context, SelectCityActivity.class);
        context.startActivity(intent);
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_city);
        ButterKnife.bind(this);

        modeList = new ArrayList<>();
        adapter = new SelectCityAdapter(getActivity(), modeList);
        listView.setAdapter(adapter);
        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
//        listView.addItemDecoration(new SpacesItemDecoration(2, new boolean[]{false, false, false, true}));
        int line = getResources().getColor(R.color.line);
        listView.addItemDecoration(new WItemDecoration(getActivity(), 1).setColor(line));
    }

    protected void asynRun() {
        modeList.add(new CityMode(1, "山东"));
        modeList.add(new CityMode(2, "北京"));
        modeList.add(new CityMode(3, "上海"));
        CityMode mode = CityMode.getMode(getActivity());
        for (CityMode cityMode : modeList) {
            if (cityMode.id == mode.id) {
                cityMode.isCheck = true;
                break;
            }
        }
        adapter.notifyDataSetChanged();
    }

    @OnClick(R.id.textR)
    public void onViewClicked() {
        CityMode defaultCityMode = adapter.getDefaultCityMode();
        CityMode.setMode(getActivity(), defaultCityMode);
        LocalBroadcastManager.getInstance(getActivity()).sendBroadcastSync(new Intent(MyConstants.ActionCityRef));
        finish();
    }
}
